using System;
using System.Data;
using System.Data.Common;

namespace uk.co.tonyditchfield.tort.utility
{
    /// <summary>
    /// Database Helper class.
    /// </summary>
    internal class DBUtility
    {
        /// <summary>
        /// Cleans up connections and datareaders associated with connection
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="reader"></param>
        internal static void CleanUp(IDbConnection connection, IDataReader reader)
        {
            CleanUp(reader);
            CleanUp(connection);
        }

        /// <summary>
        /// Cleans up connections associated with connection
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="reader"></param>
        internal static void CleanUp(IDbConnection connection)
        {
            if (!connection.Equals(null))
            {
                try
                {
                    connection.Close();
                }
                catch (DbException dbe)
                {
                    throw new exception.PersistenceException(dbe.ErrorCode, dbe);
                }
            }
        }

        /// <summary>
        /// Cleans up datareaders associated with connection
        /// </summary>
        /// <param name="connection"></param>
        /// <param name="reader"></param>
        internal static void CleanUp(IDataReader reader)
        {
            if (reader != null)
            {
                try
                {
                    reader.Close();
                }
                catch (DbException dbe)
                {
                    throw new exception.PersistenceException(dbe.ErrorCode, dbe);
                }
            }
        }
    }
}
